<?php
/**
 * Customer_group_model
 * 客户组模型
 *
 * @uses TXT_Model
 * @package
 * @copyright 2003-2011 The PHP Developer
 * @author Smiling Dolphin <dolphin@php5x.com>
 * @license PHP Version 5.2 {@link http://www.php.net/}
 * @version $id$
 */

class Customer_group_model extends TXT_Model {

    /**
     * 状态：普通
     */
    const STATUS_NORMAL = 1;

    /**
     * 状态：删除
     */
    const STATUS_REMOVE = 9;

    /**
     * _table 
     * 表名
     * 
     * @var string
     * @access private
     */
    private $_table = 'customer_groups';

    /**
     * __construct
     * 构造函数
     *
     * @access protected
     * @return void
     */
    function __construct() {

        parent::__construct();
        $this->load->database('default');
        $this->load->library('session');
    }

    /**
     * addGroup 
     * 添加客户组
     * 
     * @access public
     * @return integer
     */
    function addGroup() {

        // 如果不是POST，返回false
        if (!$this->input->is_post()) {
            return $this->_return('数据来路错误', false);
        }

        // 接收外部数据
        $uid = $this->session->userdata('uid');
        $cgName = $this->input->post('cg_name');
        $cgDesc = $this->input->post('cg_desc');

        if (!$uid || !$cgName) {
            return $this->_return('分组数据错误', false);
        }

        if ($this->_isGroupExistsByUidAndCgName($uid, $cgName)) {
            return $this->_ruturn('该分组已存在', false);
        }

        $insertData = array(
            'uid' => $uid,
            'cg_name' => $cgName,
            'cg_description' => $cgDesc,
            'cg_status' => self::STATUS_NORMAL,
            'cg_created' => date($this->config->item('log_date_format'))
        );

        $result = $this->db->insert($this->_table, $insertData);
        if($result) {
            return $this->_return('', true);
        } else {
            return $this->_return('数据库插入错误', false);
        }
    }

    /**
     * editGroup 
     * 编辑客户组
     * 
     * @param integer $cgid 组ID
     * @access public
     * @return boolean
     */
    function editGroup($cgid) {

        // 如果不是POST，返回false
        if (!$this->input->is_post()) {
            return false;
        }

        // 判断uid参数
        if (!$cgid) {
            return false;
        }

        // 接收表单数据
        $cgName = $this->input->post('cg_name');
        $cgDesc = $this->input->post('cg_desc');

        // 更新的用户数据
        $updateData = array(
            'cg_name' => $cgName,
            'cg_description' => $cgDesc,
            'cg_updated' => date($this->config->item('log_date_format'))
        );

        return $this->db->update($this->_table, $updateData, array('cgid' => $cgid));
    }

    /**
     * delete 
     * 删除客户组
     * 
     * @param integer $cgid 组ID
     * @access public
     * @return boolean
     */
    function delete($cgid) {

        $deleteData = array(
            'cg_status' => self::STATUS_REMOVE,
            'cg_deleted' => date($this->config->item('log_date_format'))
        );

        return $this->db->update($this->_table, $deleteData, array('cgid' => $cgid));
    }

    /**
     * listGroupByUid 
     * 根据用户ID列出客户组
     * 
     * @param mixed $uid 
     * @access public
     * @return void
     */
    function listGroupByUid($uid) {

        $query = $this->db->get_where($this->_table, array('uid' => $uid, 'cg_status !=' => self::STATUS_REMOVE));
        $data = array();

        foreach($query->result_array() as $row) {
            $data[] = $row;
        }

        return $data;
    }

    /**
     * incrCount 
     * 增加客户数量
     * 
     * @param integer $cgid 客户组ID
     * @param integer $count 增加数量
     * @access public
     * @return boolean
     */
    function incrCount($cgid, $count = 1) {

        $this->db->set('cg_count', 'cg_count + ' . $count, false);
        $this->db->where('cgid', $cgid);
        return $this->db->update($this->_table);
    }

    /**
     * decrCount 
     * 减少客户数量
     * 
     * @param integer $cgid 客户组ID
     * @param integer $count 减少数量
     * @access public
     * @return boolean
     */
    function decrCount($cgid, $count = 1) {

        $this->db->set('cg_count', 'cg_count - ' . $count, false);
        $this->db->where("cgid = '$cgid' AND cg_count >= '$count'");
        return $this->db->update($this->_table);
    }

    /**
     * _isGroupExistsByUidAndCgName 
     * 检查该用户是否已经存在这个组
     * 
     * @param integer $uid 用户ID
     * @param string $cgName 组名称
     * @access private
     * @return boolean
     */
    private function _isGroupExistsByUidAndCgName($uid, $cgName) {

        $query = $this->db->get_where($this->_table, array('uid' => $uid,
            'cg_name' => $cgName,
            'cg_status !=' => self::STATUS_REMOVE));

        if ($query->num_rows() > 0) {
            return true;
        } else {
            return false;
        }
    }
}
